//
//  FZSpeechVerificationView.m
//  EnglishTalk
//
//  Created by 周咏 on 16/5/16.
//  Copyright © 2016年 Feizhu Tech. All rights reserved.
//

#import "FZSpeechVerificationView.h"
#import "UIAlertView+SRBlocks.h"
#import "FCUtil.h"
#import <UMMobClick/MobClick.h>

@interface FZSpeechVerificationView()
//提示文案
@property (strong, nonatomic) UILabel * promptLabel ;

@property(strong, nonatomic)UILabel *titleLabel;
@property(strong, nonatomic)UILabel *timeOutLabel;
@property(strong, nonatomic)FZStyleSheet *css;

@property(strong, nonatomic)NSTimer *timer;

@property(assign, nonatomic)NSInteger timeOut;
@end

@implementation FZSpeechVerificationView

-(instancetype)init{
    self = [super init];
    if(self){
        self.css = [FZStyleSheet currentStyleSheet];
        //提示文案
        self.promptLabel= [[UILabel alloc] init];
        self.promptLabel.text = @"收不到验证码？试试";
        self.promptLabel.font = self.css.fontOfH4;
        self.promptLabel.textColor = self.css.color_6;
        self.promptLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:self.promptLabel];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.text = @"语音验证码";
        self.titleLabel.textAlignment = NSTextAlignmentRight;
        self.titleLabel.font = self.css.fontOfH4;
        self.titleLabel.textColor = self.css.color_6;
        [self addSubview:self.titleLabel];
        self.titleLabel.userInteractionEnabled = NO;
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapTitleLabel)];
        [self.titleLabel addGestureRecognizer:gesture];
        
        self.timeOutLabel = [[UILabel alloc] init];
        self.timeOutLabel.text = @"(60)重新获取";
        self.timeOutLabel.textAlignment = NSTextAlignmentRight;
        self.timeOutLabel.font = self.css.fontOfH3;
        self.timeOutLabel.textColor = self.css.color_1;
        self.timeOutLabel.hidden = YES;
        [self addSubview:self.timeOutLabel];
        
        WEAKSELF
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make){
            make.height.equalTo(weakSelf);
            make.trailing.equalTo(weakSelf).offset(-30);
//            make.width.equalTo(@(73));
            make.centerY.equalTo(weakSelf);
        }];
        [self.titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        
        [self.timeOutLabel mas_makeConstraints:^(MASConstraintMaker *make){
            make.height.equalTo(weakSelf);
            make.trailing.equalTo(weakSelf).offset(30);
            make.width.equalTo(@(87));
            make.centerY.equalTo(weakSelf);
        }];

        [self.promptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(weakSelf);
            make.trailing.equalTo(weakSelf.titleLabel.mas_leading).offset(0);
            make.leading.equalTo(weakSelf);
            make.centerY.equalTo(weakSelf);
        }];
        
        self.timeOut = 60;
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0)
                                                      target:self
                                                    selector:@selector(updateTimeOut)
                                                    userInfo:nil
                                                     repeats:YES];
        [self.timer setFireDate:[NSDate distantFuture]];
    }
    return self;
}

-(void)didTapTitleLabel{
    [MobClick event:@"click_speech_verification"];
    NSMutableDictionary *params;
    if(self.delegate){
        params = [self.delegate sVerParamas];
        [params setValue:@"1" forKey:@"c_type"];
    }
    
    if(params){
        if(self.codeType == 1){
            NSString *account = [params objectForKey:@"with_account"];
            if(!account || [account isEqualToString:@""]){
                [self.delegate sVerStopProgress:@"请填写正确的支付宝账号!"];
                return;
            }
        }else{
            NSString *mobile = [params objectForKey:@"mobile"];
            if (!mobile || [mobile length] < 11) {
                [self.delegate sVerStopProgress:@"请填写正确的手机号码!"];
                return;
            }
            if (![FCUtil isValidateMobilePhoneNumber:mobile]) {
                [self.delegate sVerStopProgress:@"请填写正确的手机号码!"];
                return;
            }
        }
    }else{
        return;
    }
    
    UIAlertView * alterView=[[UIAlertView alloc] initWithTitle:@"提示" message:@"我们将以电话的形式通知你要输入的验证码!" cancelButtonItem:[RIButtonItem itemWithLabel:@"取消" action:^(){
        
    }] otherButtonItems:[RIButtonItem itemWithLabel:@"确定" action:^(){
        if(self.codeType == 1){
            WEAKSELF
            [self startProgress];
//            [[[FZMyWalletService alloc] init] sendAuthCodeWithParams:params success:^(NSInteger statusCode, NSString *message, id dataObject) {
//                if (statusCode == kFZRequestStatusCodeSuccess) {
//                    [weakSelf stopProgress:nil];
//                    [weakSelf waitingCode];
//                } else {
//                    NSString *msg = message ? message : @"系统错误,请重新尝试!";
//                    [weakSelf stopProgress:msg];
//                }
//            } failure:^(id responseObject, NSError *error) {
//                [weakSelf stopProgress:@"网络错误,请重新尝试!"];
//            }];
        }else{
            WEAKSELF
            NSString *urlString = [[SRAPICreater sharedInstance] APIWithName:@"user_mobile_code"];
            [self startProgress];
            [[SRNetWorkManager sharedInstance] GET:urlString needCache:NO parameters:params responseClass:nil success:^(NSInteger statusCode, NSString *message, id responseDataObject){
                if(statusCode == kFZRequestStatusCodeSuccess){
                    [weakSelf stopProgress:nil];
                    [weakSelf waitingCode];
                    self.promptLabel.hidden = YES;
                }else{
                    NSString *msg = message ? message : @"系统错误,请重新尝试!";
                    [weakSelf stopProgress:msg];
                }
            } failure:^(id responseObject, NSError * error){
                [weakSelf stopProgress:@"网络错误,请重新尝试!"];
            }];
        }
    }], nil];
    
    [alterView show];
}

-(void)updateTimeOut{
    self.timeOut--;
    if(self.delegate && [self.delegate respondsToSelector:@selector(sVerRemainTime:)]){
        [self.delegate sVerRemainTime:self.timeOut];
    }
    if(self.timeOut <= 0){
        self.promptLabel.hidden = NO;
        [self reSet:YES];
    }else{
        self.timeOutLabel.text = [NSString stringWithFormat:@"(%ld)重新获取", (long)self.timeOut];
    }
}

-(void)setAvailable:(BOOL)available{
    if(available){
        self.titleLabel.userInteractionEnabled = YES;
        [self reSet:available];
    }else{
        self.titleLabel.userInteractionEnabled = NO;
        [self reSet:available];
    }
    
}

-(void)reSet:(BOOL)available{
    self.titleLabel.hidden = NO;
    if(available){
       self.titleLabel.textColor = self.css.color_1;
    }else{
       self.titleLabel.textColor = self.css.color_7;
    }
    self.timeOutLabel.hidden = YES;
    self.timeOut = 60;
    [self.timer setFireDate:[NSDate distantFuture]];
}

-(void)waitingCode{
    self.titleLabel.hidden = YES;
    self.timeOutLabel.textColor = self.css.color_7;
    self.timeOutLabel.hidden = NO;
     self.timeOut = 60;
    self.timeOutLabel.text = [NSString stringWithFormat:@"(%ld)重新获取", (long)self.timeOut];
    [self.timer setFireDate:[NSDate date]];
}

-(void)startProgress{
    if(self.delegate && [self.delegate respondsToSelector:@selector(sVerStartProgress)]){
        [self.delegate sVerStartProgress];
    }
}

-(void)stopProgress:(NSString*)msg{
    if(self.delegate && [self.delegate respondsToSelector:@selector(sVerStopProgress:)]){
        [self.delegate sVerStopProgress:msg];
    }
}

-(void)clear{
    if(self.timer){
        [self.timer setFireDate:[NSDate distantFuture]];
        [self.timer invalidate];
    }
}

@end
